package com.coderman.infosys.plat.auth.common.dto;

import com.coderman.infosys.plat.auth.common.error.IError;

/**
 * description: ResultDataDto <br>
 * date: 2020/7/7 0:04 <br>
 * author: coderman <br>
 * version: 1.0 <br>
 */
public class ResultDataDto<T> {
    private String  code;
    private String msg;
    private T data;
    public ResultDataDto(){}


    public boolean isSuccess(){
        return this.code == CodeEnum.SUCCESS.getCode()+"";
    }
    public ResultDataDto(String code, String msg){
        this.code = code;
        this.msg = msg;
    }
    public  ResultDataDto<T> setErrorCodeMsg(String msg){
        this.code = CodeEnum.ERROR.getCode()+"";
        this.msg = msg;
        return  this;
    }

    public ResultDataDto<T> setData(T data){
        this.data = data;
        this.code = CodeEnum.SUCCESS.getCode()+"";
        this.msg = "成功";
        return this;
    }

    public static ResultDataDto success(){
        return new ResultDataDto(CodeEnum.SUCCESS.getCode()+"","成功");
    }

    public static  <T> ResultDataDto<T> success(T data){
        ResultDataDto<T> resultDataDto = new ResultDataDto(CodeEnum.SUCCESS.getCode()+"","成功");
        return resultDataDto.setData(data);
    }

    public static  <T> ResultDataDto<T> fail(String code,String msg,T data){
        ResultDataDto<T> resultDataDto = new ResultDataDto(code,msg);
        return resultDataDto.setData(data);
    }

    public static  <T> ResultDataDto<T> fail(String code,String msg){
        ResultDataDto<T> resultDataDto = new ResultDataDto(code,msg);
        return resultDataDto;
    }

    public static  <T> ResultDataDto<T> fail(IError iError){
        ResultDataDto<T> resultDataDto = new ResultDataDto(iError.getErrorCode(),iError.getErrorMsg());
        return resultDataDto;
    }

    enum CodeEnum{
        SUCCESS(0),
        ERROR(-1),
        ;

        int code;
        CodeEnum(int code){this.code = code;}

        public int getCode() {
            return code;
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
}
